import RenderContext from "@/views/cg/lib/RenderContext.ts";

export class Mesh {
  constructor({vertexes, indexes =null, dimension = 3}) {
    this.dimension = dimension
    this.vertexes = vertexes
    this.indexes = indexes
    //
    this.gl = RenderContext.getGL()
    this.program = RenderContext.getProgram()
    this.vertexPosition = this.gl.getAttribLocation(this.program, 'a_position')

    this.vertexBuffer = null

    this.init()
  }

  init (){
    // 将Buffer和数据绑定起来 create bind data
    this.vertexBuffer = this.gl.createBuffer()
    this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer)
    this.gl.bufferData(
      this.gl.ARRAY_BUFFER,
      new Float32Array(this.vertexes),
      this.gl.STATIC_DRAW
    )
  }

  draw(){
    this.gl.enableVertexAttribArray(this.vertexPosition)
    // gl 用的是某一个bufer，每次使用 这个模型的 buffer，这样切换
    this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer)
    const type = this.gl.FLOAT
    const normalized = false
    const stride = 0
    const offset = 0
    this.gl.vertexAttribPointer(
      this.vertexPosition,
      this.dimension,
      type,
      normalized,
      stride,
      offset
    )
    this.gl.drawArrays(this.gl.TRIANGLES, 0 , this.vertexes.length / this.dimension)
  }
}
